package com.ccnf.web.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.ccnf.core.bean.Response;
import com.ccnf.core.bean.ResponseWithData;
import com.ccnf.core.bean.ResponseWithPage;
import com.ccnf.core.utils.ResultUtil;
import com.ccnf.model.entity.Exhibitor;
import com.ccnf.model.entity.SocialUser;
import com.ccnf.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户相关API
 * </p>
 *
 * @author zhangxieliu
 * @since 2018-04-13
 */
@RestController
@RequestMapping("/users")
@Api(value = "SocialUserController", description = "用户相关API")
public class SocialUserController {
    @Autowired
    private UserService userService;

    @ApiOperation("录入用户信息，如果存在用户的openId，则直接返回用户ID，否则录入信息后返回用户ID")
    @PostMapping
    public ResponseWithData save(@RequestBody SocialUser user) {
        return ResponseWithData.success(userService.saveUser(user));
    }

    @ApiOperation("通过用户ID查看用户信息，如果不存在该用户信息则返回null")
    @GetMapping("{userId}")
    public ResponseWithData get(@PathVariable Long userId) {
        return ResponseWithData.success(userService.selectById(userId));
    }

    @ApiOperation("通过会管家的用户ID查看用户信息，如果不存在该用户信息则返回null")
    @GetMapping("union/{unionId}")
    public ResponseWithData get(@PathVariable String unionId) {
        return ResponseWithData.success(userService.findByUnionId(unionId));
    }

    @ApiOperation("通过用户ID更新用户的部分信息，必须传递用户ID")
    @PatchMapping("{userId}")
    public Response modify(@PathVariable Long userId, @RequestBody SocialUser socialUser) {
        socialUser.setUserId(userId);
        ResultUtil.validateBoolean(userService.updateById(socialUser), "更新用户信息失败");
        return Response.success();
    }

    @ApiOperation("用户进行展商认证")
    @PostMapping("exhibitor/auth")
    public Response auth(@RequestBody Exhibitor exhibitor) {
        userService.saveExhibitor(exhibitor);
        return Response.success();
    }

    @ApiOperation("获取用户展商认证信息")
    @GetMapping("{userId}/exhibitor")
    public ResponseWithData exhibitor(@PathVariable Long userId,
                                      @RequestParam(required = false) Long expoId) {
        return ResponseWithData.success(userService.findExhibitorByUserId(expoId, userId));
    }

    @ApiOperation("修改用户展商认证信息")
    @PatchMapping("{userId}/exhibitor")
    public Response modifyExhibitor(@PathVariable Long userId, @RequestBody Exhibitor exhibitor) {
        exhibitor.setUserId(userId);
        userService.modifyExhibitor(exhibitor);
        return Response.success();
    }

    @ApiOperation("获取展商认证列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条，默认5", dataType = "Integer", paramType = "query")
    })
    @GetMapping("exhibitors")
    public ResponseWithPage exhibitors(
            @RequestParam(required = false) Long expoId,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = userService.findExhibitors(expoId, page);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }
}